package controllers;

import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import models.User;
import models.Course;

public class SubscribetoCourseController extends HttpServlet {

    private int id;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        if (request.getParameter("id") == null) {

            Dbmanager dbManager = new Dbmanager();
            dbManager.openConnection();
            QueryManager queryManager = new QueryManager(dbManager);
            HttpSession sessie = request.getSession();

            request.setAttribute("coursesUitSessie", queryManager.getAllCourses());
            
            if(request.getParameter("tryOut") != null) {
                sessie.setAttribute("tryOut", Boolean.parseBoolean(request.getParameter("tryOut")));
            }

            dbManager.closeConnection();

            RequestDispatcher dispatcher = request.getRequestDispatcher("/subscribe_to_course.jsp");
            dispatcher.forward(request, response);
        } else {
            id = Integer.parseInt(request.getParameter("id"));
            doPost(request, response);
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Dbmanager dbManager = new Dbmanager();
        dbManager.openConnection();
        QueryManager queryManager = new QueryManager(dbManager);
        HttpSession sessie = request.getSession();



        User user = (User) sessie.getAttribute("gebruikerinfo");
        request.setAttribute("gebruikerinfo", user);
        Course course;

        course = queryManager.getCoursbyid(id);
        request.setAttribute("courseinfo", course);

        dbManager.closeConnection();
        RequestDispatcher dispatcher = request.getRequestDispatcher("/subscribe_confirmation.jsp");
        dispatcher.forward(request, response);

    }
}
